package com.bdmc.car.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.car.domain.CarGarageItem;
import com.bdmc.car.domain.bo.CarGarageItemBo;
import com.bdmc.car.domain.vo.CarGarageItemVo;
import com.bdmc.car.mapper.CarGarageItemMapper;
import com.bdmc.car.service.ICarGarageItemService;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 维修项Service业务层处理
 *
 * @author bdmc
 * @date 2023-01-06
 */
@Service
public class CarGarageItemServiceImpl extends ServicePlusImpl<CarGarageItemMapper, CarGarageItem, CarGarageItemVo> implements ICarGarageItemService {

    @Override
    public CarGarageItemVo queryById(Integer itemId){
        return getVoById(itemId);
    }

    @Override
    public TableDataInfo<CarGarageItemVo> queryPageList(CarGarageItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CarGarageItem> lqw = buildQueryWrapper(bo);
        Page<CarGarageItemVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<CarGarageItemVo> mySelectPage(CarGarageItemBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<CarGarageItemVo> queryList(CarGarageItemBo bo) {
        LambdaQueryWrapper<CarGarageItem> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<CarGarageItem> buildQueryWrapper(CarGarageItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CarGarageItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGarageId() != null, CarGarageItem::getGarageId, bo.getGarageId());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), CarGarageItem::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getItemCount()), CarGarageItem::getItemCount, bo.getItemCount());
        lqw.eq(bo.getItemPrice() != null, CarGarageItem::getItemPrice, bo.getItemPrice());
        return lqw;
    }

    @Override
    public Boolean insertByBo(CarGarageItemBo bo) {
        CarGarageItem add = BeanUtil.toBean(bo, CarGarageItem.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setItemId(add.getItemId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(CarGarageItemBo bo) {
        CarGarageItem update = BeanUtil.toBean(bo, CarGarageItem.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CarGarageItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
